package org.nulldevice.tankshot;


import java.util.ArrayList;

import org.nulldevice.tankshot.ai.BasicAI;
import org.nulldevice.tankshot.entities.EnemyTank;
import org.nulldevice.tankshot.entities.Item;
import org.nulldevice.tankshot.entities.Tank;
import org.nulldevice.tankshot.entities.Wall;

public class Game implements Runnable {

	private static final double DELTAFACTOR = 50;

	public static final int DIRECTION_UP = 0x1, DIRECTION_DOWN = 0x2,
			DIRECTION_LEFT = 0x4, DIRECTION_RIGHT = 0x8;

	public static int gameSizeX = 800, gameSizeY = 600;

	protected boolean doExit = false;
	
	GameContainer container = GameContainer.getInstance();

	Tank playerTank;


	public Game() {
		// Initialize the entities in our game so there's something
		// to see at startup
		initEntities();
	}

	private void initEntities() {
		final GameContainer container = GameContainer.getInstance();

		playerTank = new Tank(100, 100, 98, 46, "Player", "tank1.png",
				"tank1cannon.png");
		final EnemyTank tank1 = new EnemyTank(80, 480, 98, 46, "PC1",
				"tank2.png", "tank2cannon.png", new BasicAI());
		final EnemyTank tank2 = new EnemyTank(200, 300, 98, 46, "PC2",
				"tank2.png", "tank2cannon.png", new BasicAI());
		final ArrayList<Tank> enemies = new ArrayList<Tank>();
		enemies.add(playerTank);
		tank1.setEnemyTanks(enemies);
		tank2.setEnemyTanks(enemies);


		container.addItem(new Wall(10, 300, 10, 600));
		container.addItem(new Wall(750, 300, 10, 600));

		container.addItem(new Wall(400, 5, 800, 10));
		container.addItem(new Wall(400, 530, 800, 10));

		container.addItem(new Wall(300, 140, 20, 120));
		container.addItem(new Wall(220, 210, 240, 20));
		
		container.addItem(new Wall(500, 310, 90, 10));
		container.addItem(new Wall(500, 310, 10, 90));
		
		container.addItem(tank1);
		container.addItem(tank2);
		container.addItem(playerTank);

		
	}

	/**
	 * Start a fresh game, this should clear out any old data and create a new
	 * set.
	 */
	public void prepareGame() {
		// clear out any existing entities and initialize a new set
		container.clear();
		initEntities();
	}

	public void run() {
		final GameContainer container = GameContainer.getInstance();
		final ArrayList<Item> destroyItems = new ArrayList<Item>();
		long lastLoopTime = System.currentTimeMillis();
		long delta;

		if (container.getItems() == null) {
			System.out.println("This should never happen: There are no items in the game.");
			return;
		}

		while (!doExit) {
			// work out how long its been since the last update, this
			// will be used to calculate how far the entities should
			// move this loop
			container.removeDestroyedItems();
			delta = System.currentTimeMillis() - lastLoopTime;
			lastLoopTime = System.currentTimeMillis();


			for (final ArrayList<? extends Item> list : container.getItems()) {
				for (int i = 0; i < list.size(); i++) {
					final Item item = list.get(i);
					if (item.isAboutToExplode()) {
						item.explode(delta / DELTAFACTOR);
					} else {
						if (item.move(delta / DELTAFACTOR)) {
							// only do a collision check if the item moved
							GameContainer.getInstance().checkHit(item);
						}
					}
					if (item.isDestroyed())
						destroyItems.add(item);
				}
			}


			container.addDestroyedItems(destroyItems);
			destroyItems.clear();


			try {
				Thread.sleep(10);
			} catch (final InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public void setMousePosition(final int x, final int y) {
		if (playerTank != null)
			this.playerTank.setAimingPoint(x, y - 28);
	}

	public int getPlayerDirection() {
		return this.playerTank.getDirection();
	}

	public void setPlayerDirection(final int d) {
		this.playerTank.setDirection(d);
	}

	public void exit() {
		this.doExit = true;
	}

	public void playerPlacesMine() {
		this.playerTank.placeMine();
		
	}

	public void playerShoots() {
		this.playerTank.shoot();
	}
}
